package com.my.control
{
	import com.my.control.validate.RetrievePassValidate;
	import com.my.service.RemoteServiceFactory;
	import com.my.view.popup.RetrievePassView;
	
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	import mx.rpc.events.ResultEvent;
	
	
	public class RetrievePassControl extends AbstractControl implements IControl
	{
		private static var instance : RetrievePassControl = null;
		
		private var retrievePassValidate : RetrievePassValidate;
		
		public function RetrievePassControl(value:UIComponent)
		{
			super(value);
			retrievePassValidate = new RetrievePassValidate(value);
		}
		
		public static function getInstance():RetrievePassControl{
			return instance;
		}
		
		public static function startup(value : UIComponent):void{
			instance = new RetrievePassControl(value);
		}

		public override function addEventListener():void{
			getView().ok.addEventListener(MouseEvent.CLICK,okHandler);
		}
		
		public function okHandler(event:MouseEvent):void{
			if(retrievePassValidate.validate()){
				var username : String = getView().username.text;
				var isLockCombo : String = getView().isLockCombo.value.toString();
				var myResult : String = getView().myResult.text;
				
				RemoteServiceFactory.getInstance().userInfoService.addEventListener(ResultEvent.RESULT,function(e:ResultEvent):void{
					RemoteServiceFactory.getInstance().userInfoService.removeEventListener(ResultEvent.RESULT,arguments.callee);
					
					var v : int = e.result as int;
					switch(v){
						case -1:
							getView().newPassword.text = "些用户名不存在.";
							getView().newPassword2.text = "";
							break;
						case -2:
							getView().newPassword.text = "此用户名没有这个问题";
							getView().newPassword2.text = "";
							break;
						case -3:
							getView().newPassword.text = "回答错误.";
							getView().newPassword2.text = "";
							break;
						case 1:
							getView().newPassword.text = "您的密码以重置为123456.";
							getView().newPassword2.text =  "快去登录后修改密码.";
							break;
					}
					
				});
				RemoteServiceFactory.getInstance().userInfoService.retrievePass(username,isLockCombo,myResult);
			}
		}
		
		public function getView():RetrievePassView{
			return RetrievePassView(view);
		}
	}
}